package net.Reality.Android.Ant;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;


public class Instruments extends Dialog implements android.view.View.OnClickListener
{
	private static final int GRID_LAYOUT_WIDTH = 100;
	private static final int GRID_LAYOUT_HEIGHT = 100;
	
	Activity context; 
	GridView view;
	SharedPreferences number;
	Editor editor;
	int table;
	int ind = 4;
	AntObject object;
	ImageLoder loder;
	int pos1;
	int pos2;
	int pos3;
	int size = 4;
	
	public Instruments(final Context context, final ImageLoder loder) 
	{
		super(context);
		this.context = (Activity) context;
		setContentView(R.layout.instruments);
		number = context.getSharedPreferences(context.getString(R.string.shared_preferences_name), 1);
		editor = number.edit();
		view = (GridView) findViewById(R.id.gridview);
		
		this.loder = loder;
		
		setGridViewParameters();
		
		setTitle("Instruments:");
		
		view.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView parent, View v, int position, long id) 
            {    
            	ind -= 1;
            	
            	switch(ind)
            	{
            	case 3: pos1 = position;
            			size = 3;
            			break;
            	case 2: pos2 = position;
            			size = loder.getFromTable(pos1).getFromTable(pos2).getSizes().size();
            			break;
            	case 1: pos3 = position;
            			break;
            	}
            	
            	if(ind == 1)
            	{
	                try
	                {
	                	editor.putInt(context.getString(R.string.shared_preferences_object_name), pos1);
	                	editor.putInt(context.getString(R.string.shared_preferences_object_type), pos2);
	                	editor.putInt(context.getString(R.string.shared_preferences_object_size), pos3);
	                	editor.commit();
	                	cancel();
	                }
	                catch(Exception ex)
	                {
	                	ex.printStackTrace();
	                }
            	}
            	else
            	{
            		((BaseAdapter) view.getAdapter()).notifyDataSetChanged();
            	}
            }
        });
		
		view.setAdapter(new BaseAdapter()
        {
        	public View getView(int position, View convertView, ViewGroup parent) 
        	{
        		// TODO Auto-generated method stub
        		
        		ImageView imageView = null;
        		TextView textView = null;
        		View view = null;
        		LayoutInflater vi = null;
        		
        		if (convertView == null) 
                {
        			vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	                view = vi.inflate(R.layout.gridviewitem, null); 
                }
        		else
        		{
        			view = convertView;
                }
        		
        		try
        		{
	         		imageView = (ImageView) view.findViewById(R.id.instr_imageView);
	         		textView = (TextView) view.findViewById(R.id.instr_textView);
      		
	         		object = setPointToTable(position);
		           	imageView.setImageBitmap(object.getIcon());
		           	textView.setText(object.getDisplayName());
		           	
		           	
		           	imageView.setScaleType(ImageView.ScaleType.FIT_XY);
		           	view.setPadding(8, 8, 8, 8);
		           	view.setLayoutParams(new GridView.LayoutParams(GRID_LAYOUT_WIDTH, GRID_LAYOUT_HEIGHT));
        		}
        		catch(Exception ex)
        		{
        			// some mistakes
        		}
        		
        		
     			return view;
        	}
        	@Override
			public int getCount() 
			{
				// TODO Auto-generated method stub
        		
        		return size;
			}

			@Override
			public Object getItem(int arg0) 
			{
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public long getItemId(int arg0) 
			{
				// TODO Auto-generated method stub
				return 0;
			}
        });
    }
	
	private AntObject setPointToTable(int position)
	{
		Log.e("dddddd", Integer.toString(size));
		
		switch(ind)
		{
			case 4:	return loder.getFromTable(position);
			
			case 3: return loder.getFromTable(pos1).getFromTable(position); 
				
			case 2: return loder.getFromTable(pos1).getFromTable(pos2).getSizes().get(position); 
		}
			
		return null;
	}
	
	
	private void setGridViewParameters()
    {
	    Display display = context.getWindowManager().getDefaultDisplay();
    	view.setNumColumns(display.getWidth() / 100);
    }
	

	@Override
	public void onClick(View v) 
	{
		// TODO Auto-generated method stub
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
	    if (keyCode == KeyEvent.KEYCODE_BACK) 
	    {
	    	cancel();
	        return super.onKeyDown(keyCode, event);
	    }
	    return super.onKeyDown(keyCode, event);
	}
	
}

